package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DIshController {
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    @ApiOperation("新增菜品")
    @PostMapping
    @CacheEvict(cacheNames = "dish_cache", allEntries = true)
    public Result newDishes(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        //新增菜品
        dishService.newDishes(dishDTO);

        return Result.success();
    }

    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result page(DishPageQueryDTO dto){
        //菜品分页查询
        PageResult page = dishService.page(dto);
        return Result.success(page);
    }

    @ApiOperation("菜品起售、停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dish_cache", allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id){
        dishService.startOrStop(id,status);
        return Result.success();
    }

    /**
     * 通过菜品id查找菜品相关数据
     * @param id
     * @return
     */
    @ApiOperation("通过菜品id查找菜品相关数据")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){
        DishVO dishVO = dishService.queryDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 批量删除菜品
     */
    @ApiOperation("批量删除菜品相关数据")
    @DeleteMapping
    @CacheEvict(cacheNames = "dish_cache", allEntries = true)
    public Result deleteBatchDish(@RequestParam List<Long> ids){
        log.info("批量删除菜品信息:{}",ids);
        dishService.deleteBatchDish(ids);
        return Result.success();
    }

    @ApiOperation("菜品更新")
    @PutMapping
    @CacheEvict(cacheNames = "dish_cache", key = "'dish'+#dishDTO.id")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        dishService.updateDishWithFlavor(dishDTO);
        return Result.success();
    }

    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result list(String categoryId){
        List<Dish> list = dishService.queryDishByCategoryId(categoryId);
        return Result.success(list);
    }

}
